package net.orange.console.model.analyzer;

import java.util.Collection;

import net.orange.console.model.dataset.ConsoleData;
import net.orange.console.model.dataset.ConsoleMetaData;
import net.orange.console.model.dataset.ConsoleRow;

/**
 * The analyze table data calculator, It get the result according one set of the
 * console data & what the calculation method in analyze table.
 * 
 */
public class Calculator {

	/**
	 * Compute the result according set of the console data & what the
	 * calculation method in analyze table
	 * 
	 * @param datas
	 *            The set of the console data.
	 * @param method
	 *            The calculation method
	 * @param metaData
	 *            The target field.
	 * @return The numeric result.
	 */
	public double compute(Collection<ConsoleRow> datas,
			CalculationMethod method, ConsoleMetaData metaData) {
		if (method == CalculationMethod.SUM)
			return sum(datas, metaData);
		return 0;
	}

	/**
	 * Sum all the data
	 * 
	 * @param datas
	 *            The data
	 * @param metaData
	 *            The target field
	 * @return The total amount
	 */
	public double sum(Collection<ConsoleRow> datas, ConsoleMetaData metaData) {
		if (datas == null)
			return 0;
		
		int totalValue = 0;

		for (ConsoleRow row : datas) {
			ConsoleData consoleData = row.getData(metaData);
			
			totalValue += (consoleData != null) ? consoleData.getNumericValue()
					: 0;
		}
		return totalValue;
	}
}
